<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态线程池设计指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            background: rgba(255, 255, 255, 0.95);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 600;
            margin-right: 12px;
            flex-shrink: 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        ::selection {
            background: rgba(102, 126, 234, 0.3);
            color: #333;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                动态线程池设计指南
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                掌握高并发场景下的线程资源管理艺术，构建能够自适应调整的智能线程池
            </p>
            <div class="mt-10 flex justify-center gap-8">
                <div class="text-center">
                    <i class="fas fa-cogs text-3xl mb-2"></i>
                    <p class="text-sm opacity-80">动态调整</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-chart-line text-3xl mb-2"></i>
                    <p class="text-sm opacity-80">性能优化</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shield-alt text-3xl mb-2"></i>
                    <p class="text-sm opacity-80">资源保护</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="card-hover rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                设计一个能够动态调整线程数的自定义线程池是一项复杂的任务，尤其是在高并发和大规模场景下，合理管理线程资源和避免资源竞争尤为关键。本指南将深入探讨如何构建一个既高效又灵活的动态线程池。
            </p>
        </div>

        <!-- Architecture Visualization -->
        <div class="card-hover rounded-2xl p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap text-gradient mr-3"></i>
                线程池架构概览
            </h2>
            <div class="mermaid">
                graph TB
                    A[任务提交] --> B{线程池}
                    B --> C[任务队列]
                    B --> D[工作线程集合]
                    B --> E[监控组件]
                    C --> F[BlockingQueue]
                    D --> G[Worker线程]
                    E --> H[动态调整器]
                    H --> I[扩展线程]
                    H --> J[缩减线程]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Core Components -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="section-number">1</span>
                定义核心结构
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card-hover rounded-xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">任务队列</h3>
                    <p class="text-gray-600 leading-relaxed">
                        存储待执行任务的核心容器，支持阻塞操作，确保线程安全的任务存取。
                    </p>
                </div>
                <div class="card-hover rounded-xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-users-cog"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">工作线程集合</h3>
                    <p class="text-gray-600 leading-relaxed">
                        管理所有活跃的工作线程，支持动态增减，确保资源的高效利用。
                    </p>
                </div>
                <div class="card-hover rounded-xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-ban"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">拒绝策略</h3>
                    <p class="text-gray-600 leading-relaxed">
                        当系统资源达到上限时的任务处理策略，保护系统免于过载。
                    </p>
                </div>
                <div class="card-hover rounded-xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">动态调整策略</h3>
                    <p class="text-gray-600 leading-relaxed">
                        根据系统负载和任务队列状态，智能调整线程池规模。
                    </p>
                </div>
            </div>
        </section>

        <!-- Basic Implementation -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="section-number">2</span>
                实现基本的线程池功能
            </h2>
            <div class="space-y-6">
                <div class="highlight-box">
                    <h3 class="text-xl font-semibold mb-3">
                        <i class="fas fa-inbox text-gradient mr-2"></i>
                        任务队列选择
                    </h3>
                    <p class="text-gray-700 mb-3">
                        使用<code class="bg-gray-100 px-2 py-1 rounded">BlockingQueue</code>实现，常见选择包括：
                    </p>
                    <ul class="space-y-2 ml-6">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-2 py-1 rounded">LinkedBlockingQueue</code>：无界队列，适合任务量不确定的场景</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-2 py-1 rounded">ArrayBlockingQueue</code>：有界队列，可控制内存占用</span>
                        </li>
                    </ul>
                </div>
                
                <div class="highlight-box">
                    <h3 class="text-xl font-semibold mb-3">
                        <i class="fas fa-users text-gradient mr-2"></i>
                        工作线程管理
                    </h3>
                    <p class="text-gray-700">
                        维护线程池中所有活动线程，使用<code class="bg-gray-100 px-2 py-1 rounded">HashSet&lt;Worker&gt;</code>存储，
                        其中<code class="bg-gray-100 px-2 py-1 rounded">Worker</code>是实现了<code class="bg-gray-100 px-2 py-1 rounded">Runnable</code>接口的内部类。
                    </p>
                </div>
            </div>
        </section>

        <!-- Dynamic Adjustment -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="section-number">3</span>
                动态调整线程数的机制
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card-hover rounded-xl p-6 text-center">
                    <i class="fas fa-tasks text-4xl text-gradient mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">基于任务队列长度</h3>
                    <p class="text-gray-600 text-sm">
                        监控队列长度，超过阈值时增加线程，低于下限时减少线程
                    </p>
                </div>
                <div class="card-hover rounded-xl p-6 text-center">
                    <i class="fas fa-clock text-4xl text-gradient mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">基于任务处理时间</h3>
                    <p class="text-gray-600 text-sm">
                        分析任务平均处理时间，响应时间过长时动态扩容
                    </p>
                </div>
                <div class="card-hover rounded-xl p-6 text-center">
                    <i class="fas fa-microchip text-4xl text-gradient mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">基于系统资源</h3>
                    <p class="text-gray-600 text-sm">
                        根据CPU、内存使用率智能调整线程数量
                    </p>
                </div>
            </div>
            
            <div class="mt-8 bg-yellow-50 border-l-4 border-yellow-400 p-